﻿@namespace MudBlazor
@inherits MudComponentBase
@using MudBlazor.Resources
@typeparam T

@inject InternalMudLocalizer Localizer

@if (IsOnlyHeader)
{
    <th scope="col" class="mud-table-cell @Classname" style="" @attributes="@UserAttributes">
        @ChildContent
    </th>
}
else if (Column != null && !Column.HiddenState.Value)
{
    <th @ref=@_headerElement scope="col" class="@Classname" style="@Stylename" colspan="@Column.HeaderColSpan" @attributes="@UserAttributes">
        @if (DataGrid.DragDropColumnReordering)
        {
            <MudDropZone CanDrop="@((item) => (Column.DragAndDropEnabled ?? true))" ItemDisabled="@((item) => !item.DragAndDropEnabled ?? false)" T="Column<T>" Identifier="@(Column.PropertyName ?? _id)">

            </MudDropZone>
        }
        else
        {
            @TableHeader()
        }
        @if (resizable)
        {
            <div @onpointerdown=OnResizerPointerDown @onpointerover=OnResizerPointerOver @onpointerleave=OnResizerPointerLeave
                 class="@ResizerClass" style="@ResizerStyle" />
        }
    </th>
}

@code {

    internal RenderFragment TableHeader()
    {
        return
        @<text>
        <span class="column-header">
            @if (sortable)
            {
                <span class="@SortHeaderClass" @onclick="SortChangedAsync">
                    @if (Column.HeaderTemplate != null)
                    {
                        @Column.HeaderTemplate(Column.headerContext)
                    }
                    else if (IncludeHierarchyToggle)
                    {
                        @HierarchyToggle
                    }
                    else 
                    {
                        @computedTitle
                    }
                </span>
            }
            else
            { 
                @if (Column.HeaderTemplate != null)
                {
                    @Column.HeaderTemplate(Column.headerContext)
                }
                else if (IncludeHierarchyToggle)
                {
                    @HierarchyToggle
                }
                else 
                {
                    @computedTitle
                }
            }
            <span class="@OptionsClass">
                @if (sortable)
                {
                    if (SortDirection == SortDirection.None)
                    {
                        <MudIconButton Icon="@Column.SortIcon" Class="@sortIconClass" Size="@Size.Small" OnClick="@SortChangedAsync" aria-label="@Localizer[LanguageResource.MudDataGrid_Sort]"></MudIconButton>
                    }
                    else
                    {
                        <MudIconButton Icon="@Column.SortIcon" Class="@sortIconClass" Size="@Size.Small" OnClick="@SortChangedAsync" aria-label="@Localizer[LanguageResource.MudDataGrid_Sort]"></MudIconButton>
                    }

                    if (DataGrid.SortMode == SortMode.Multiple)
                    {
                        if (Column.SortIndex < 0)
                        {
                            <span class="mud-sort-index mud-text-disabled invisible">@(Column.SortIndex + 1)</span>
                        }
                        else
                        {
                            <span class="mud-sort-index mud-text-disabled">@(Column.SortIndex + 1)</span>
                        }
                    }
                }

                @if (DataGrid.FilterMode != DataGridFilterMode.ColumnFilterRow)
                {
                    if (hasFilter)
                    {
                        <MudIconButton Class="filter-button filtered" Icon="@DataGrid.FilterIconFilled" Size="@Size.Small" OnClick="@OpenFilters" aria-label="@Localizer[LanguageResource.MudDataGrid_OpenFilters]"></MudIconButton>
                    }
                    else if (showFilterIcon)
                    {
                        <MudIconButton Class="filter-button" Icon="@DataGrid.FilterIconEmpty" Size="@Size.Small" OnClick="@AddFilter" aria-label="@Localizer[LanguageResource.MudDataGrid_AddFilter]"></MudIconButton>
                    }
                }

                @if (showColumnOptions)
                {
                    <MudMenu Icon="@Icons.Material.Filled.MoreVert" Size="Size.Small" Dense="true" AriaLabel="@Localizer[LanguageResource.MudDataGrid_ShowColumnOptions]">
                        <MudMenuItem Disabled="@(SortDirection == SortDirection.None)" OnClick="@RemoveSortAsync">@Localizer[LanguageResource.MudDataGrid_Unsort]</MudMenuItem>
                        @if (filterable && DataGrid.FilterMode != DataGridFilterMode.ColumnFilterRow)
                        {
                            <MudMenuItem OnClick="@AddFilter">@Localizer[LanguageResource.MudDataGrid_Filter]</MudMenuItem>
                        }
                        @if (hideable)
                        {
                            <MudMenuItem OnClick="@HideColumnAsync">@Localizer[LanguageResource.MudDataGrid_Hide]</MudMenuItem>
                        }
                        @if (groupable)
                        {
                            if (Column?.GroupingState.Value ?? false)
                            {
                                <MudMenuItem OnClick="@UngroupColumnAsync">@Localizer[LanguageResource.MudDataGrid_Ungroup]</MudMenuItem>
                            }
                            else
                            {
                                <MudMenuItem OnClick="@GroupColumnAsync">@Localizer[LanguageResource.MudDataGrid_Group]</MudMenuItem>
                            }
                        }
                    </MudMenu>
                }
                @if (DataGrid.DragDropColumnReordering && (Column.DragAndDropEnabled ?? true))
                {
                    <MudIconButton Icon="@DataGrid.DragIndicatorIcon" Size="@DataGrid.DragIndicatorSize" Class="drag-icon-options"></MudIconButton>
                }
            </span>
        </span>
        @if (filterable && DataGrid.FilterMode == DataGridFilterMode.ColumnFilterMenu)
        {
            <MudOverlay @bind-Visible="@_filtersMenuVisible" AutoClose />
            <MudPopover UserAttributes="@DataGrid?.PositionAttributes" Open="@_filtersMenuVisible" AnchorOrigin="@Origin.BottomRight" TransformOrigin="@Origin.TopRight" Class="pa-4 column-filter-popup mud-popover-position-override">
                @if (Column.FilterTemplate != null)
                {
                    @Column.FilterTemplate(Column.FilterContext) 
                }
                else
                {
                    <MudGrid>
                        <MudItem xs="12">
                            @DataGrid.Filter(Column.FilterContext.FilterDefinition, Column)
                        </MudItem>
                        <MudItem xs="12" Class="d-flex justify-end">
                            <MudButton Class="clear-filter-button" OnClick="@ClearFilterAsync">@Localizer[LanguageResource.MudDataGrid_Clear]</MudButton>
                            <MudButton Class="apply-filter-button" Color="@Color.Primary" OnClick="@ApplyFilterAsync">@Localizer[LanguageResource.MudDataGrid_Filter]</MudButton>
                        </MudItem>
                    </MudGrid>
                }
            </MudPopover>
        }
        </text>;
    }

    private RenderFragment HierarchyToggle =>
        @<text>
             <MudIconButton Icon="@GetGroupIcon()"
                            Class="ma-n3 pa-1 mud-hierarchy-toggle-button"
                            OnClick="@ToggleHierarchyAsync"
                            aria-label="@(Expanded ? @Localizer[LanguageResource.MudDataGrid_CollapseAllGroups] : @Localizer[LanguageResource.MudDataGrid_ExpandAllGroups])"/>
         </text>;

}
